package com.bnyy.medicalHousekeeper.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.bean.Order;

import java.util.ArrayList;
import java.util.function.Consumer;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/9
 **/
public class OrderAdapter extends BaseNormalListAdapter<Order, OrderAdapter.ViewHolder> {

    public OrderAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.item_order, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        Order order = getData(position);

        holder.tvShopName.setText(order.getShop_name());
        holder.tvAmount.setText("共" + order.getTotal() + "件");
        holder.tvTotalPrice.setText("合计¥" + order.getTotal_money());

        if (TextUtils.isEmpty(order.getLabel())) {
            holder.tvTag.setVisibility(View.GONE);
        } else {
            holder.tvTag.setVisibility(View.VISIBLE);
            if (holder.tvTag.getBackground() == null) {
                GradientDrawable bg = new GradientDrawable();
                bg.setCornerRadius(10);
                bg.setColor(Color.parseColor("#FFF9F2"));
                holder.tvTag.setBackground(bg);
            }
            holder.tvTag.setText(order.getLabel());
        }
        holder.llGoods.removeAllViews();
        ArrayList<Order.OrderGoods> orderGoodsList = order.getGoods_list();
        orderGoodsList.forEach(new Consumer<Order.OrderGoods>() {
            @Override
            public void accept(Order.OrderGoods orderGoods) {
                View item = inflater.inflate(R.layout.item_order_goods, holder.llGoods, false);
                ImageView ivGoods = item.findViewById(R.id.iv_goods);
                TextView tvGoodsName = item.findViewById(R.id.tv_goods_name);
                TextView tvTime = item.findViewById(R.id.tv_time);

                GlideHelper.setRoundImage(mContext, orderGoods.getGoods_image(), ivGoods);
                tvGoodsName.setText(orderGoods.getGoods_name());
                tvTime.setText(orderGoods.getPay_time());

                holder.llGoods.addView(item);

                inflater.inflate(R.layout.divider_with_margin, holder.llGoods, true);
            }
        });
    }

    static class ViewHolder extends BaseNormalListAdapter.ViewHolder {

        TextView tvShopName;
        TextView tvTag;
        TextView tvAmount;
        TextView tvTotalPrice;
        LinearLayout llGoods;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvShopName = itemView.findViewById(R.id.tv_shop_name);
            tvTag = itemView.findViewById(R.id.tv_tag);
            tvAmount = itemView.findViewById(R.id.tv_amount);
            tvTotalPrice = itemView.findViewById(R.id.tv_total_price);
            llGoods = itemView.findViewById(R.id.ll_goods);
        }
    }
}
